#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: zhengpingping

import sys
import xlrd3
import importlib
import ExcelRead
importlib.reload(sys)

outPath = '../../StarForce/Assets/Scripts/Lua/Data/Table/'
#内部使用
search_names = []

#获取字段类型名称
def get_type_name(typeRow):
    if typeRow == 'bool':
        return 'boolean'
    elif typeRow == 'int':
        return 'number'
    elif typeRow == 'array_int':
        return 'number[]' 
    else:
        return 'string'

#生成lua代码
def create_item_luaCode(ws):
    search_line = ExcelRead.excel_sheet_read(ws)
    if(len(search_line) <= 0):
        print(ws.name + '  data is empty')
        return

    begin_line = search_line[0]
    if(begin_line.fieldType != 'int' and begin_line.fieldType != 'string'):
        print(begin_line.fieldName + ' type can only is int or string')
        return

    string = ('---@class Table_@NameItem')
    string += ('\r\n') + ('local Table_@NameItem = BaseClass("Table_@NameItem", IDataItem)')
    string += ('\r\n')
    string += ('\r\n') + ('function Table_@NameItem:__ctor()')

    #生成数据
    for line in search_line:
        if line.fieldType == 'bool':
            string += ('\r\n\t---') + '@type boolean @' + line.fieldDesc
            string += ('\r\n\t') + ('self.@NameRow = false')
        elif line.fieldType == 'int':
            string += ('\r\n\t---') + '@type number @' + line.fieldDesc
            string += ('\r\n\t') + ('self.@NameRow = 0')
        elif line.fieldType == 'array_int':
            string += ('\r\n\t---') + '@type number[] @' + line.fieldDesc
            string += ('\r\n\t') + ('self.@NameRow = {}')
        else:
            string += ('\r\n\t---') + '@type string @' + line.fieldDesc
            string += ('\r\n\t') + ('self.@NameRow = ""')
        string = string.replace("@NameRow", line.fieldName)

    string += ('\r\n') + ('end')
    string += ('\r\n')
    string += ('\r\n') + ('function Table_@NameItem:Load(reader)')

    #读取数据
    for line in search_line:
        if line.fieldType == 'bool':
            string += ('\r\n\t') + ('self.@NameRow = reader:ReadBool()')
        elif line.fieldType == 'int':
            string += ('\r\n\t') + ('self.temp = reader:ReadShortString()')
            string += ('\r\n\t') + ('self.@NameRow = tonumber(self.temp)')
        elif line.fieldType == 'array_int':
            string += ('\r\n\t') + ('self.temp = reader:ReadShortString()')
            string += ('\r\n\t') + ('self.@NameRow = Utility.StringToNumbers(self.temp)')
        else:
            string += ('\r\n\t') + ('self.@NameRow = reader:ReadString()')
        string = string.replace("@NameRow", line.fieldName)
    # string += ('\r\n\t') + ('self.temp = nil')
    string += ('\r\n') + ('end')

    string += ('\r\n')
    string += ('\r\n') + ('---@class Table_@NameMgr : IDataMgr')
    string += ('\r\n') + ('local this = BaseClass("Table_@NameMgr", IDataMgr)')

    string += ('\r\n')
    string += ('\r\n') + ('function this:FileName()')
    string += ('\r\n\t') + ('return "Txt/@Name.txt"')
    string += ('\r\n') + ('end')

    string += ('\r\n')
    string += ('\r\n') + ('---@return Table_@NameItem')
    string += ('\r\n') + ('function this:GetItemByKey(key)')
    string += ('\r\n\t') + ('return self.super.GetItemByKey(self, key)')
    string += ('\r\n') + ('end')

    string += ('\r\n')
    string += ('\r\n') + ('---@return Table_@NameItem')
    string += ('\r\n') + ('function this:GetItemByIdx(idx)')
    string += ('\r\n\t') + ('return self.super.GetItemByIdx(self, idx)')
    string += ('\r\n') + ('end')

    string += ('\r\n')
    string += ('\r\n') + ('function this:Load(reader)')
    string += ('\r\n\t') + ('local item = Table_@NameItem()')
    string += ('\r\n\t') + ('item:Load(reader)')
    string += ('\r\n\t') + ('self.mItemDic[item.@begin_name] = self.mDataCount')
    string += ('\r\n\t') + ('table.insert(self.mItemList, item)')
    string += ('\r\n') + ('end')

    string += ('\r\n')
    string += ('\r\n') + ('---@type Table_@NameMgr')
    string += ('\r\n') + ('Table_@NameMgr = this()')
    string = string.replace("@Name", ws.name)
    string = string.replace("@begin_name", begin_line.fieldName)
    
    search_names.append(ws.name)
    outputName = ("Table_@NameMgr.lua").replace("@Name", ws.name)
    ExcelRead.save_file(outPath + outputName, string)

#生成mgr代码
def create_mgr_luaCode():
    string = ('---@class TableDataMgr : Singleton')
    for name in search_names:
        temp = '---@field public @NameMgr Table_@NameMgr'
        string += ('\r\n') + temp.replace("@Name", name)

    string += ('\r\n') + ('local this = BaseClass("TableDataMgr", Singleton)')
    string += ('\r\n')
    string += ('\r\n') + ('function this:__ctor()')

    for name in search_names:
        temp = 'self.@NameMgr = require("Data/Table/Table_@NameMgr")'
        string += ('\r\n\t') + temp.replace("@Name", name)

    string += ('\r\n') + ('end')
    string += ('\r\n')
    string += ('\r\n') + ('function this:Init()')

    for name in search_names:
        string += ('\r\n\t') + ('self.@NameMgr:Init()').replace("@Name", name)

    string += ('\r\n') + ('end')
    string += ('\r\n')
    string += ('\r\n') + ('---@type TableDataMgr')
    string += ('\r\n') + ('TableDataMgr = this:GetInstance()')

    outputName = outPath + 'TableDataMgr.lua'
    ExcelRead.save_file(outputName, string)

# 读取excel文件
def excel_sheet_read(file):
    wb = xlrd3.open_workbook(file)
    for sheet in wb.sheets():
        print(sheet.name + ' translate start')
        if(ExcelRead.check_fileName(sheet.name, search_names)):
            create_item_luaCode(sheet)

# 初始化方法
ExcelRead.init(outPath, excel_sheet_read)
create_mgr_luaCode()
print('TableDataMgr translate finished')